<!-- src/App.vue -->
<template>
    <el-container style="height: 100vh; background-color: #f3f4f6;">
      <el-header style="background-color: #2c3e50; color: white; text-align: center;">
        <h1>基于智普清言配置的AI电力助手</h1>
      </el-header>
      <el-main>
        <el-row :gutter="20">
          <el-col :span="12" :offset="6">
            <el-card class="box-card">
              <template #header>
                <div class="card-header">
                  <span>与小助手对话</span>
                </div>
              </template>
              <div class="chat-container">
                <el-scrollbar class="chat-scroll" ref="chatScroll">
                  <!-- 聊天内容展示区域 -->
                  <div v-for="message in messages" :key="message.id" class="chat-message">
                    <div :class="message.sender === 'user' ? 'user-message-container' : 'ai-message-container'">
                      <img
                          :src="message.sender === 'user' ? userAvatar : aiAvatar"
                          :alt="message.sender === 'user' ? 'User Avatar' : 'AI Avatar'"
                          class="avatar"
                      />
                      <div :class="message.sender === 'user' ? 'user-message' : 'ai-message'">
                        <span v-html="message.text"></span>
                      </div>
                    </div>
                  </div>
                  <div v-if="loading" class="chat-message ai-message">
                    AI正在处理数据中，请稍候...
                  </div>
                </el-scrollbar>
                <div class="chat-footer">
                  <el-input
                      v-model="userInput"
                      placeholder="请输入消息..."
                      @keyup.enter="sendMessage"
                      :disabled="loading"
                  ></el-input>
                  <el-button type="primary" @click="sendMessage" :loading="loading">发送</el-button>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </el-main>
    </el-container>
  </template>
  
  <script>
  import { ref, onMounted, nextTick } from 'vue';
  import { callAI } from '@/api/ai/ai';
  import userAvatar from '@/assets/images/qq.png'; // 替换为实际的用户头像路径
  import aiAvatar from '@/assets/images/xiaobing.png'; // 替换为实际的AI头像路径
  export default {
    name: 'App',
    setup() {
      const messages = ref([]);
      const userInput = ref('');
      const loading = ref(false);
      const chatScroll = ref(null);
  
      const sendMessage = async () => {
        const userMessage = userInput.value.trim();
        if (userMessage !== '') {
          messages.value.push({
            id: messages.value.length,
            sender: 'user',
            text: userMessage
          });
  
          userInput.value = ''; // 清空输入框
          loading.value = true; // 开始加载状态
  
          try {
            const aiResponse = await callAI({ message: userMessage });
            let aiR  = JSON.stringify(aiResponse)
            aiR = aiR.match(/content=(.*?)(?=,|$)/)[1]
            // 替换 \n 为 <br>
            aiR = aiR.replace(/\\n/g, '<br>'); // 注意这里是双反斜杠
            // 将标题加粗并去掉两边的**
            aiR = aiR.replace(/(\d+\.\s)\*\*(.*?)\*\*/g, '$1<b>$2</b>');
  
            // 假设 aiResponse.reply 包含了 AI 的回复
            messages.value.push({
              id: messages.value.length,
              sender: 'ai',
              text: aiR
            });
          } catch (error) {
            console.error('Error sending message to AI:', error);
            messages.value.push({
              id: messages.value.length,
              sender: 'error',
              text: '无法发送消息，请稍后再试。'
            });
          } finally {
            loading.value = false; // 结束加载状态
            nextTick(() => {
              chatScroll.value.setScrollTop(chatScroll.value.$el.scrollHeight);
            });
          }
        }
      };
  
      onMounted(() => {
        nextTick(() => {
          chatScroll.value.setScrollTop(chatScroll.value.$el.scrollHeight);
        });
      });
  
      return {
        messages,
        userInput,
        sendMessage,
        loading,
        chatScroll,
        userAvatar,
        aiAvatar
      };
    }
  };
  </script>
  
  <style>
  body {
    font-family: 'Roboto', sans-serif;
    background-color: #f3f4f6;
    margin: 0;
    padding: 0;
  }
  
  .el-header {
    background-color: #2c3e50;
    color: white;
    text-align: center;
    line-height: 60px;
  }
  
  .card-header {
    text-align: center;
    font-size: 1.2em;
    font-weight: bold;
    color: #2c3e50;
  }
  
  .chat-container {
    height: 600px;
    position: relative;
    background-color: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    padding: 20px;
  }
  
  .chat-scroll {
    height: calc(100% - 60px); /* 减去 footer 的高度 */
    overflow-y: auto;
  }
  
  .chat-message {
    display: flex; /* 使用flex布局 */
    align-items: flex-start; /* 对齐方式为顶部对齐 */
    margin-bottom: 12px;
    word-wrap: break-word; /* 允许长单词换行 */
  }
  
  .user-message-container {
    justify-content: flex-end; /* 用户消息右对齐 */
  }
  
  .ai-message-container {
    justify-content: flex-start; /* AI 消息左对齐 */
  }
  
  .user-message {
    color: #333;
    background-color: #e6f7ff;
    display: inline-block; /* 使背景颜色仅覆盖文本内容 */
    padding: 10px;
    border-radius: 8px;
    margin-left: 10px;
  }
  
  .ai-message {
    color: #2c3e50;
    background-color: #ecf0f1;
    display: inline-block; /* 使背景颜色仅覆盖文本内容 */
    padding: 10px;
    border-radius: 8px;
    margin-right: 10px;
  }
  
  .avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
  }
  
  .chat-footer {
    position: absolute;
    bottom: 0;
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 12px;
    box-sizing: border-box;
    background-color: #fff;
    border-top: 1px solid #ebeef5;
  }
  
  .el-input__inner {
    border-color: #2c3e50;
  }
  
  .el-button--primary {
    background-color: #2c3e50;
    border-color: #2c3e50;
  }
  
  .el-button--primary:hover {
    background-color: #34495e;
    border-color: #34495e;
  }
  </style>
  